home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / pvm34b3.zip / pvm34b3 / pvm3 / examples / gexample.f < prev    next >
Text File  |  1997-07-22  |  6KB  |  191 lines

  1. C
  2. C $Id: gexample.f,v 1.2 1997/06/27 19:52:49 pvmsrc Exp $
  3. C
  4. C Example of some group function and reduction functions in PVM 
  5. C SPMD style program
  6. C
  7. C 11 March 1994 - Creation by P. Papadopoulos (phil@msr.epm.ornl.gov)
  8. C
  9. C
  10. C
  11.       program gexample
  12.  
  13.       implicit none
  14.       include '../include/fpvm3.h'
  15.  
  16. C --- Set Initial and Default Parameters
  17.       integer    DEF_DIMENSION, INITTAG,SUMTAG,PRODTAG
  18.       parameter (DEF_DIMENSION = 100       )
  19.       parameter (INITTAG       = 1000     )
  20.       parameter (SUMTAG        = INITTAG+1)
  21.       parameter (PRODTAG       = INITTAG+2)
  22.  
  23.       integer mytid, myinst, nproc, maxmax, root
  24.       integer dimension, ninst, bufid
  25.       integer tids(32)
  26.       integer nsibs
  27.       integer nhost, narch, dtid, speed
  28.       integer blksize, nextra, mysrow, i, j, itemp, info
  29.       integer subblock(DEF_DIMENSION,DEF_DIMENSION)
  30.       integer colsum(DEF_DIMENSION)
  31.       real*8  colprod(DEF_DIMENSION)
  32.       character*32 host, arch
  33.       logical spmd
  34.     
  35.  
  36. C --- External declarations of PVM and User defined reduce function
  37.       external PvmSum 
  38.       external calcprod 
  39.  
  40. C ---------------- Begin Program -----------------------------------------
  41.       spmd = .false.
  42.  
  43. c     Enroll in PVM and join a group
  44.       call pvmfmytid( mytid )
  45.  
  46. C    Try to determine if we were spawned spmd-style 
  47.  
  48.       call pvmfsiblings(nsibs, 0, tids(1))
  49.       if (nsibs > 1) spmd = .true.
  50.     
  51.       call pvmfjoingroup( 'matrix', myinst )
  52.       if( myinst .lt. 0 ) then
  53.         call pvmfperror( 'joingroup: ', info)
  54.         call pvmfexit( info )
  55.         stop
  56.       endif
  57.  
  58. c     Set matrix size and number of tasks.
  59.       call pvmfconfig( nhost, narch, dtid, host, arch, speed, info )
  60.       nproc = 2*nhost
  61.       if( nproc .gt. 32 ) nproc = 32
  62.       dimension = DEF_DIMENSION
  63.  
  64.       if( myinst .eq. 0 ) then              
  65.         print* 
  66.         print*, 'This program demonstrates some group and reduction'
  67.         print*, 'operations in PVM.  The output displays the' 
  68.         print*, 'the product of the first column of a 100x100 Toeplitz'
  69.         print*, 'matrix and the matrix 1-norm. The matrix data is'
  70.         print*, 'distributed among several tasks.  The Toeplitz'
  71.         print*, 'matrix is symmetric with the first row being the'
  72.         print*, 'row vector [1 2 ... n].'
  73.         print* 
  74.  
  75. c       Start up more copies of myself
  76.         if(nproc  .gt.  1 .and. .not.spmd)  then
  77.  
  78.           print*, 'There are ',nhost, ' machines in the configuration'
  79.           print*, 'Starting ',nproc - 1, ' tasks'
  80.  
  81.           call pvmfspawn( 'fgexample', PVMDEFAULT, '*', 
  82.      >                    nproc -1, tids, ninst ) 
  83.           if( ninst .lt. nproc-1 ) then
  84.             print*, 'Trouble in spawn. Check tids'
  85.             print*, tids
  86.             call pvmflvgroup( 'matrix', info )
  87.             call pvmfexit( info )
  88.           endif  
  89.         endif  
  90.  
  91.         if ( spmd ) nproc = nsibs
  92.         print*, ' --> using ', nproc, ' processors <--'
  93.         print*
  94.  
  95.       endif  
  96.  
  97.  
  98. c     Wait till everyone has joined the group and freeze it  
  99.       call pvmffreezegroup( 'matrix', nproc, info )
  100.  
  101. c     Broadcast input data to all members
  102.       if( myinst .eq. 0 ) then              
  103.         call pvmfinitsend( PVMDEFAULT, bufid )
  104.         call pvmfpack(INTEGER4, nproc, 1, 1, info) 
  105.         call pvmfpack(INTEGER4, dimension, 1, 1, info )
  106.         call pvmfbcast( 'matrix', INITTAG ,info ) 
  107.       else
  108.         call pvmfrecv( -1, INITTAG, info )
  109.         call pvmfunpack( INTEGER4, nproc, 1, 1 ,info)
  110.         call pvmfunpack( INTEGER4, dimension, 1, 1, info)
  111.       endif  
  112.  
  113. C     Map matrix rows to processors --       
  114.       blksize =  dimension/nproc 
  115.       nextra =   mod(dimension, nproc) 
  116.       if( myinst .lt.  nextra ) then 
  117.          mysrow = 1 + (blksize + 1) * myinst  
  118.          blksize = blksize + 1 
  119.       else
  120.          mysrow = 1+ (blksize + 1)*(nextra) + blksize*(myinst - nextra)
  121.       endif 
  122.       if( mysrow .gt. dimension)  then  
  123.         blksize = 0
  124.       endif 
  125.     
  126. C     Assign data to this subblock.  The entries below make the entire matrix
  127. C     a symmetric Toeplitz matrix (i.e. diagonals are of constant value)  
  128.       do j=1, dimension
  129.         do i=1, blksize
  130.           subblock(i,j) = abs(mysrow + i - j) 
  131.         end do
  132.       end do
  133.  
  134. C     Locally compute the sum of each column and put into colsum  
  135.       do j=1, dimension
  136.         colsum(j) = 0
  137.         colprod(j) = 1.0
  138.       end do 
  139.       do j=1, dimension
  140.         do i=1,blksize
  141.           itemp =  abs ( subblock(i,j) )
  142.           colsum(j) = colsum(j) + itemp
  143.           colprod(j) = colprod(j) * itemp
  144.         end do
  145.       end do
  146.  
  147. C     Get global sum by calling reduce using PvmSum 
  148.       root = 0
  149.       call pvmfreduce( PvmSum, colsum, dimension, INTEGER4, SUMTAG,
  150.      >                 'matrix', root, info) 
  151.  
  152. c     Here is example of supplying a user-defined OP to reduce
  153.       call pvmfreduce( calcprod, colprod, dimension, REAL8, PRODTAG,
  154.      >                 'matrix', root, info) 
  155.  
  156. c     Root prints out result
  157.       if( myinst .eq. root ) then
  158.         maxmax = 0
  159.         do j=1,dimension
  160.           maxmax = max(colsum(j),maxmax)
  161.         end do
  162.         write(6,*) ' The 1-Norm is ', maxmax 
  163.         write(6,1000)  dimension
  164.         write(6,*) ' The product of column 1 is', colprod(1)
  165.         write(6,1001) dimension
  166.       endif  
  167. 1000  format(' (Should be the sum of the first ', I3, ' integers)')
  168. 1001  format(' (Should be ', I3, ' factorial)')
  169.  
  170. c     Problem done. Be sure all members have finished computation before exit.
  171.       call pvmfbarrier( 'matrix', nproc, info)
  172.       call pvmflvgroup( 'matrix', info)
  173.       call pvmfexit( info )
  174.       stop
  175.       end
  176.  
  177. c----------------------------------------------------------------------------
  178. C *** Example of a User-defined Reduction Function ***/
  179.  
  180.       subroutine calcprod( datatype, x, y, num, info )
  181.       integer datatype
  182.       real*8 x(num), y(num)
  183.       integer num, info
  184.   
  185.       integer i
  186.       do i=1,num
  187.          x(i) = x(i) * y(i)
  188.       end do
  189.       return
  190.       end
  191.